package com.cykj.service.impl;

import com.cykj.mapper.AdminMapper;
import com.cykj.mapper.PatientMapper;
import com.cykj.pojo.CyAdmin;
import com.cykj.pojo.CyPatient;
import com.cykj.service.AdminService;
import com.cykj.util.JWTUtils;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.cykj.util.excel.ExcelOutputUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public ResponseDto deleteUserByIds(List<Integer> ids) {
        int i = adminMapper.deleteUserByIds(ids);
        if (i > 0){
            return new ResponseDto(1,"操作成功",i);
        }else {
            return new ResponseDto(0,"操作失败",null);
        }
    }

    @Override
    public ResponseDto selectByData(int sort,String value,int pageCurr,int pageSize) {
        List<CyAdmin> admin = adminMapper.selectByData(sort,value,pageCurr,pageSize);
        if (admin != null){
            return new ResponseDto(1,"操作成功",admin);
        }else {
            return new ResponseDto(0,"查无此人",null);
        }
    }

    @Override
    public ResponseDto login(String account, String pwd) {
        CyAdmin admin = adminMapper.login(account, pwd);
        if (admin != null){
            //登录成功生成一个token
            HashMap<String, Object> data = new HashMap<>();//存取token的数据
            data.put("adminInfo",admin);
            String token = JWTUtils.generateToken(data);
            data.put("token",token);
            return new ResponseDto(1,"登录成功",data);
        }else {
            return new ResponseDto(0,"登录失败",null);
        }
    }

    @Override
    public ResponseDto register(String account, String pwd, String name,int roleId) {
        CyAdmin login = adminMapper.login(account, pwd);
        //注册之前就要判断用户是否存在
        if (login == null){
            int register = adminMapper.register(account, pwd, name,roleId);
            if (register != 0){
                return new ResponseDto(1,"注册成功",register);
            }else {
                return new ResponseDto(0,"注册失败",null);
            }
        }else {
            return new ResponseDto(-1,"该用户已经存在",null);
        }
    }

    @Override
    public ResponseDto forgetPwd(String account, String name, String newPwd, int roleId) {
        System.out.println(account+"---"+name+"----"+newPwd+"---"+roleId);
        int res = adminMapper.forgetPwd(account, name, newPwd, roleId);
        System.out.println(res);
        if (res != 0){
            return new ResponseDto(1,"重置成功",res);
        }else {
            return new ResponseDto(0,"操作失败",null);
        }
    }

    @Override
    public ResponseDto getFace(int id) {
        String face = adminMapper.getFace(id);
        if (face == null){
            return new ResponseDto(0,"图片获取失败",null);
        }else{
            face = "http://124.71.221.237:8080/test"+face;
            return new ResponseDto(1,"获取头像成功",face);
        }

    }

    @Override
    public ResponseDto alterPwd(int id, String newPwd) {
        int i = adminMapper.alterPwd(id, newPwd);
        return new ResponseDto(1,"修改成功",i);
    }

    @Override
    public ResponseDto changeStatus(int proStatus, int adminId) {
        int i = adminMapper.changeStatus(proStatus, adminId);
        if (i != 0){
            return new ResponseDto(1,"修改成功",i);
        }else {
            return new ResponseDto(0,"修改失败",null);
        }
    }

    @Override
    public ResponseDto deleteAdmin(int adminId) {
        int i = adminMapper.deleteAdmin(adminId);
        return new ResponseDto(1,"删除成功",i);
    }

    @Override
    public ResponseDto alterAdmin(int id, String name, String account, String departmentName, String roleName) {
        //先判断是否有修改
        int admin = adminMapper.alterIsExist(id, name, account, departmentName, roleName);
        System.out.println(admin);
        if (admin != 0){
        //    说明未修改
            return new ResponseDto(-1,"信息未修改",null);
        }
        //执行到这边说明已经修改了
        String roleId = adminMapper.alterRoleIsExist(roleName);
        if (roleId == null){
            //    说明要修改的角色不存在
            return new ResponseDto(-2,"角色不存在",null);
        }
        String departmentId = adminMapper.alterDepartIsExist(departmentName);
        System.out.println(departmentId);
        if (departmentId == null){
            //说明要科室的角色不存在
            return new ResponseDto(-3,"科室不存在",null);
        }
        //    如果执行到这说明没问题，那就继续修改
        int i = adminMapper.alterAdmin(id, name, account, Integer.parseInt(departmentId), Integer.parseInt(roleId));
        if (i == 0){
            return new ResponseDto(0,"修改失败",i);
        }else {
            return new ResponseDto(1,"修改成功",i);
        }
    }

    //添加工作人员
    @Override
    public ResponseDto addCommit(String adminAccount, String adminPwd, String adminName, String roleName,String departmentName,String status) {
        //添加首先判断所输入的角色名称是否存在
        String roleId = adminMapper.alterRoleIsExist(roleName);
        if (roleId == null){
            return new ResponseDto(-1,"输入的角色名称不存在",null);
        }
        //如果执行到这就说明角色名称是可以的
        //那接下俩就判断科室的名称是否正确
        String departId = adminMapper.alterDepartIsExist(departmentName);
        if (departId == null){
            return new ResponseDto(-2,"输入的科室名称不存在",null);
        }
        //执行到这就说明输入的科室和角色都是没问题的
        //那就进行角色的信息查询，判断信息是否已经添加过了
        int i1 = adminMapper.addCommitIsExist(adminAccount, adminPwd, adminName, Integer.parseInt(departId), Integer.parseInt(roleId));
        if (i1 != 0){
            //说明角色已经添加过了
            return new ResponseDto(-3,"该信息已经存在",null);
        }
        int i = adminMapper.addCommit(adminAccount, adminPwd, adminName, Integer.parseInt(departId),
                Integer.parseInt(roleId), Integer.parseInt(status));
        if (i != 0){
            return new ResponseDto(1,"添加成功",null);
        }else {
            return new ResponseDto(0,"添加失败",null);
        }
    }

    @Override
    public ResponseDto getAdmins(int pageCurr, int pageSize) {
        //1.启动分页拦截器
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyAdmin> admins = adminMapper.getAdmins();
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }
    @Override
    public void handleFileDownload(String newPath){
        List<CyPatient> allPatient = patientMapper.getAllPatient();
        //将所获得到的文件写入进去
        ExcelOutputUtil.writeToExcel(allPatient,newPath);

    }
}
